package com.luoyuanxiangvip.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoyuanxiangvip.common.annotation.Cache;
import com.luoyuanxiangvip.common.enums.ConfigEnum;
import com.luoyuanxiangvip.common.vo.*;
import com.luoyuanxiangvip.core.entity.Config;
import com.luoyuanxiangvip.core.mapper.ConfigMapper;
import com.luoyuanxiangvip.core.service.IConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019-07-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements IConfigService {

    /**
     * 根据key查询返回对象
     *
     * @param key   key
     * @return 对象
     */
    @Override
    public <T> T getValue(String key) {
        Config one = this.getOne(new QueryWrapper<Config>().eq("rel_key",key));
        Map<String, Class> map = new HashMap<>(5);
        map.put(ConfigEnum.SITE_SETTINGS.getKey(), SiteSettings.class);
        map.put(ConfigEnum.ADMIN_USER_INFO.getKey(), AdminUserInfo.class);
        map.put(ConfigEnum.QI_NIU_YUN.getKey(), QiNiuYun.class);
        map.put(ConfigEnum.OTHER_OPTION.getKey(), OtherOption.class);
        map.put(ConfigEnum.BLACK_OPTION.getKey(), BlackOption.class);
        return (T) JSONObject.parseObject(one.getRelValue(), map.get(key));
    }

    /**
     * 获取系统配置
     *
     * @return OptionVO
     */
    @Override
    @Cache(value = "【config】获取系统配置",expire = 7, unit = TimeUnit.DAYS)
    public OptionVO getOption() {
        SiteSettings siteSettings = this.getValue(ConfigEnum.SITE_SETTINGS.getKey());
        BlackOption blackOption = this.getValue(ConfigEnum.BLACK_OPTION.getKey());
        OptionVO option = new OptionVO();
        BeanUtils.copyProperties(siteSettings, option);
        option.setIps(Optional.ofNullable(blackOption.getIpsBlack())
                .map(ips -> Arrays.asList(ips.split(",")))
                .orElse(null))
                .setMails(Optional.ofNullable(blackOption.getMailsBlack())
                        .map(mails -> Arrays.asList(mails.split(",")))
                        .orElse(null))
                .setKeywords(Optional.ofNullable(blackOption.getKeywordsBlack())
                        .map(keywords -> Arrays.asList(keywords.split(",")))
                        .orElse(null));
        return option;
    }

    /**
     * 获取站长信息
     *
     * @return AdminUserInfo
     */
    @Override
    @Cache(value = "【config】获取站长信息",expire = 7, unit = TimeUnit.DAYS)
    public AdminUserInfo getAdminUserInfo() {
        return this.getValue(ConfigEnum.ADMIN_USER_INFO.getKey());
    }
}
